from osv import osv, fields
import pooler

from tools import config
from tools.translate import _



class kdvn_project(osv.osv):
    _inherit='kdvn.project'
    _name="kdvn.project"
    
    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        if name:
            context['search_field'] = 'code'
            context['operator'] = '='
            prj_ids = self.search(cr, uid, [('code', '=', name)]+args, limit=limit, context=context)
            if not prj_ids:
                context['operator'] = 'ilike'
                prj_ids = self.search(cr, uid, [('code', 'ilike', name)]+args, limit=limit, context=context)
            if not prj_ids:
                context['search_field'] = 'name'
                prj_ids = self.search(cr, uid, [('name', 'ilike', '%%%s%%' % name)]+args, limit=limit, context=context)
        else:
            prj_ids = self.search(cr, uid, args, limit=limit, context=context)
        
        result = self.name_get(cr, uid, prj_ids, context=context)
        
        if prj_ids and 'return_context' in context:
            if 'search_field' in context:
                result.append({'search_field':context['search_field'],'operator':context['operator']})
        return result
    
    def search(self, cr, user, args, offset=0, limit=None, order=None,  context=None, count=False):
        chk_over = False
        var_tmp = str(args).strip().replace(" ","").lower()
        if var_tmp:
            condition = "('check_over','=',true)"
            if var_tmp.find(condition)>0:
                cr.execute("Select Distinct\
                                kp.id\
                            from\
                                kdvn_project kp\
                            left join\
                                kdvn_report_project_budget_status krpbs on kp.id = project_id \
                            where \
                                coalesce(krpbs.over,False)\
                            group by kp.id")
                res = cr.fetchall()
                res1=[]
                for tmp in res:
                    res1.append(tmp[0])
                var_filter=res1
                args.append(('id','in',var_filter))
        if context:
            if 'rop_order_id' in context.keys():
                order_id = context['rop_order_id']
                if order_id:
                    cr.execute("Select Distinct\
                                        pol.project_id\
                                from\
                                    purchase_order_line pol\
                                left join\
                                    purchase_order po on pol.order_id=po.id\
                                where pol.order_id=%s and state not in ('draft','cancel','done')" % order_id)
                    res = cr.fetchall()
                    res1=[]
                    for tmp in res:
                        res1.append(tmp[0])
                    var_filter=res1
                    args.append(('id','in',var_filter))
                else:
                    args.append(('id','in',[]))
        return super(kdvn_project,self).search(cr, user, args, offset, limit, order,context, count)
kdvn_project()

class kdvn_contract_client(osv.osv):
    _name = "kdvn.contract.client"
    _description="KDVN Contract Client"
    _inherit='kdvn.contract.client'
    
    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        if name:
            context['search_field'] = 'contract_no'
            context['operator'] = '='
            ctc_ids = self.search(cr, uid, [('contract_no', '=', name)]+args, limit=limit, context=context)
            if not ctc_ids:
                context['operator'] = 'ilike'
                ctc_ids = self.search(cr, uid, [('contract_no', 'ilike', name)]+args, limit=limit, context=context)
            if not ctc_ids:
                context['search_field'] = 'description'
                ctc_ids = self.search(cr, uid, [('description', 'ilike', '%%%s%%' % name)]+args, limit=limit, context=context)
        else:
            ctc_ids = self.search(cr, uid, args, limit=limit, context=context)
        result = self.name_get(cr, uid, ctc_ids, context=context)
        if 'return_context' in context:
            if 'search_field' in context:
                result.append({'search_field':context['search_field'],'operator':context['operator']})
        return result

kdvn_contract_client()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
